#### FIGURE D.1: BEHAVIORAL OUTCOME
#### Study 3 persistence on quasi-behavioral measure

rm(list = ls())
source("./2_code/00_setup.R")

#### STUDY 3, WAVE 1 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)
data3$employed <- as.factor(data3$employed)

# Behavioral outcome
lin_behavior_economic3 <- lm_lin(quasi_behavior ~ treat_economic, covariates = ~
                                   edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_behavior_humanitarian3 <- lm_lin(quasi_behavior ~ treat_humanitarian, covariates = ~
                                       edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 2 ####

data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)

# Behavioral outcome
lin_behavior_economic3b <- lm_lin(quasi_behavior ~ treat_economic, covariates = ~
                                    edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_behavior_humanitarian3b <- lm_lin(quasi_behavior ~ treat_humanitarian, covariates = ~
                                        edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])


#### PREPARE DATA FOR PLOTTING ####

### Economic treatment, behavior ###

out_economic_behavior <- rbind(
  tidy(lin_behavior_economic3) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_behavior_economic3b) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error)
)

types_economic <- data.frame(
  treatment = rep('economic', 2),
  Study = c('Wave 1', 'Wave 2')
)

out_economic_behavior <- cbind(out_economic_behavior, types_economic)


### Humanitarian treatment, behavior ###

out_humanitarian_behavior <- rbind(
  tidy(lin_behavior_humanitarian3) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_behavior_humanitarian3b) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error)
)

types_humanitarian <- data.frame(
  treatment = rep('humanitarian', 2),
  Study = c('Wave 1', 'Wave 2')
)

out_humanitarian_behavior <- cbind(out_humanitarian_behavior, types_humanitarian)


#### PLOTTING ####

fla <- rbind(
  out_economic_behavior,
  out_humanitarian_behavior
)

data_plot <- data.frame(
  fla,
  Outcome = c(rep("ATE Positive Behavior", 2),
              rep("ATE Positive Behavior", 2)),
  Treatment = c(rep("Economic Narrative", 2), rep("Humanitarian Narrative", 2))
)

# Reorder factors
data_plot$Study <- factor(data_plot$Study, levels = c("Wave 2", "Wave 1"))

plot_beh <- ggplot(data_plot, aes(y = Outcome, x = estimate, color = Study, group = Study)) +
  geom_errorbarh(aes(xmin = estimate - 1.96 * std.error, xmax = estimate + 1.96 * std.error),
                 height = 0, position = position_dodge(width = 0.3), linewidth = 0.9) +
  geom_errorbarh(aes(xmin = estimate - 1.645 * std.error, xmax = estimate + 1.645 * std.error),
                 height = 0, position = position_dodge(width = 0.3), linewidth = 1.9) +
  geom_point(size = 3, position = position_dodge(width = 0.3)) +
  facet_grid(~ Treatment) +
  theme_minimal() +
  ylab("") + xlab("") +
  scale_x_continuous(limits = c(-0.05, 0.3)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  guides(color = guide_legend(reverse = TRUE)) +
  labs(color = "Study 3") +
  theme(
    strip.text = element_text(size = 16),
    axis.text.y = element_text(size = 13),
    axis.text.x = element_text(size = 13),
    legend.text = element_text(size = 13),
    legend.title = element_text(size = 13)
  )

plot_beh

ggsave(filename = paste0(plot_path, "figure_D1.png"), plot = plot_beh, width = 10, height = 4, dpi = 600)
